package com.aitaoxue.palx.db;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.aitaoxue.palx.AppContext;

public class GoodDBHelper {

	private static final int DB_VERSION = 1;
	private static final String DB_PATH = "/data/data/"
			+ AppContext.getInstance().getPackageName() + "/databases/";

	private static String DB_NAME = "goods.db";
	private static String ASSETS_NAME = "goods.db";

	public static void initDataBase() {
		boolean dbExist = checkDataBase();

		if (!dbExist) {
			try {
				File dir = new File(DB_PATH);
				if (!dir.exists()) {
					dir.mkdirs();
				}

				File dbf = new File(DB_PATH + DB_NAME);
				if (dbf.exists()) {
					dbf.delete();
				}
				SQLiteDatabase.openOrCreateDatabase(dbf, null);

				try {
					copyDataBase();
				} catch (IOException e) {
					//
				}

			} catch (Exception e) {
				// TODO: handle exception
			}
		}
	}

	private static boolean checkDataBase() {
		File dbFile = new File(DB_PATH, DB_NAME);

		if (!dbFile.exists()) {
			return false;
		}

		SQLiteDatabase db = null;

		db = SQLiteDatabase.openDatabase(DB_PATH + DB_NAME, null,
				SQLiteDatabase.OPEN_READONLY
						| SQLiteDatabase.NO_LOCALIZED_COLLATORS);

		if (db != null) {
			db.close();
		}

		return db != null ? true : false;
	}

	private static void copyDataBase() throws IOException {
		InputStream myInput = AppContext.getInstance().getAssets()
				.open(ASSETS_NAME);

		String outFileName = DB_PATH + DB_NAME;
		OutputStream myOutput = new FileOutputStream(outFileName);

		byte[] buffer = new byte[1024];
		int length = 0;
		while ((length = myInput.read(buffer)) > 0) {
			myOutput.write(buffer, 0, length);
		}

		myOutput.flush();
		myOutput.close();
		myInput.close();
	}

	private static SQLiteDatabase getDatabase() {
		SQLiteDatabase db = null;

		db = SQLiteDatabase.openDatabase(DB_PATH + DB_NAME, null,
				SQLiteDatabase.NO_LOCALIZED_COLLATORS);

		return db;
	}

	public static HashMap<String, String> getAllCate() {
		HashMap<String, String> map = new HashMap<>();

		SQLiteDatabase db = getDatabase();

		Cursor cursor = db.rawQuery("select * from cate", null);
		for (cursor.moveToFirst(); !cursor.isLast(); cursor.moveToNext()) {
			String key = cursor.getString(cursor.getColumnIndex("Field_1"));
			String value = cursor.getString(cursor.getColumnIndex("Field_2"));
			map.put(key, value);
		}

		String key = cursor.getString(cursor.getColumnIndex("Field_1"));
		String value = cursor.getString(cursor.getColumnIndex("Field_2"));
		map.put(key, value);

		db.close();
		cursor.close();
		return map;
	}

	public static List<String> getKindByCate(String id) {
		List<String> params = new ArrayList<String>();

		SQLiteDatabase db = getDatabase();

		Cursor cursor = db.rawQuery("select Field_2 from kind where Field_3="
				+ id, null);

		for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
			params.add(cursor.getString(cursor.getColumnIndex("Field_2")));
		}

		cursor.close();
		db.close();

		return params;
	}

	public static String getKindIdFromName(String name) {
		SQLiteDatabase db = getDatabase();

		Cursor cursor = db.rawQuery("select Field_1 from kind where Field_2='"
				+ name + "'", null);

		cursor.moveToFirst();
		String item = cursor.getString(0);

		return item;
	}

}
